package com.example.demo.api.controller;

import com.example.demo.dto.EmployeeDTO;
import com.example.demo.entity.Employee;
import com.example.demo.service.EmployeeService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author honeypeach
 * @date 2023/3/24 15:16
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    private final EmployeeService employeeService;

    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @PostMapping("/select")
    public ResponseEntity<?> selectById(@RequestParam("id") Long id){

        Employee employee = employeeService.findEmployeeById(id);

        System.out.println("employee: "+employee);
        return ResponseEntity.ok(employee);
    }

    @GetMapping("/{employeeId}")
    public ResponseEntity<?> list(@PathVariable("employeeId") Long employeeId) {
        Employee employee = employeeService.findEmployeeById(employeeId);
        return ResponseEntity.ok(employee);
    }


    @PostMapping("/getLeader")
    public ResponseEntity<?> getEmployeeWithLeader(@RequestBody EmployeeDTO employeeDTO){
        Employee employee = employeeService.getEmployeeWithLeader(employeeDTO);
        System.out.println("leader: "+employee);
        return ResponseEntity.ok(employee);
    }

    @PostMapping("/getEmployeeList")
    public ResponseEntity<?> getEmployeeList(@RequestBody EmployeeDTO employeeDTO){
        List<Employee> employeeList = employeeService.listEmployees(employeeDTO);
        System.out.println("employees: "+employeeList);
        return ResponseEntity.ok(employeeList);

    }

    @DeleteMapping("/delete")
    public ResponseEntity<?> delete(@RequestParam("id") Long employeeId){

       int flag =  employeeService.delete(employeeId);
       return ResponseEntity.ok(flag);
    }

    @PutMapping("/insert")
    public ResponseEntity<?> insert(@RequestBody Employee employee){

        int flag = employeeService.insertEmployee(employee);
        return ResponseEntity.ok(flag);

    }

}
